package CONF;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Classe de conexão com o banco de dados
 */

public class ConexaoOracle {
    public static String host;
    public static String porta;
    public static String base;
    public static String usuario;
    public static String senha;
    public static Connection conexao;
    public static boolean conectado;

    public ConexaoOracle() {
        host    = "projbd.cjsuqqljgoo6.sa-east-1.rds.amazonaws.com";
        porta   = "1521";
        base    = "PROJDB";
        usuario = "oracledbadmin";
        senha   = "bric*usp";
    }    
    
    public ConexaoOracle(
            String host, 
            String porta, 
            String base, 
            String usuario, 
            String senha) {
        ConexaoOracle.host      = host;
        ConexaoOracle.porta     = porta;
        ConexaoOracle.base      = base;
        ConexaoOracle.usuario   = usuario;
        ConexaoOracle.senha     = senha;
    }
    
    public static Connection conectar() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            //System.out.println("Driver JBCD não encontrado.");
            //e.printStackTrace();
            ConexaoOracle.conectado = false;
            return null;
        }
        
        ConexaoOracle.conexao = null;
        
        try {
            ConexaoOracle.conexao = DriverManager.getConnection(
                    "jdbc:oracle:thin:@" + ConexaoOracle.host + ":"+ ConexaoOracle.porta +":" + ConexaoOracle.base,
                    ConexaoOracle.usuario,
                    ConexaoOracle.senha
            );
        } catch (SQLException e) {
            //System.out.println("Falha na conexão!");
            //e.printStackTrace();
            ConexaoOracle.conectado = false;
            return null;
        }
        
        if (ConexaoOracle.conexao != null) {
            //System.out.println("Conectado com sucesso!");
            ConexaoOracle.conectado = true;
            return ConexaoOracle.conexao;
        } else {
            ConexaoOracle.conectado = false;
            return null;
	}
    }
    
    public static Connection Desconectar() {
        
        try {
            ConexaoOracle.conexao.close();    
        } catch (SQLException e) {
            //e.printStackTrace();          
        }
        
        return null;
    }
}